import os
from .document import aDoc
from .stepStringReplace import StepStringReplace
import Import
import Part
import uuid
loadFromPart = ['.brep', '.igs', '.iges', '.IGS', '.IGES']
class Step:
    def openFile(fileName):
        replace = StepStringReplace()
        real = None
        with open(fileName, 'rb') as f:
            blob = f.read()
            real = replace.feed(blob)
        print('openFile:', fileName)
        (file, ext) = os.path.splitext(fileName)
        tmpFile = str(uuid.uuid4()) + '.' + ext
        with open(tmpFile, 'wb+') as f:
            f.write(real)

        aDoc.cleanObjects()
        # results = Import.insert(tmpFile, aDoc.doc.Name)
        if(ext in loadFromPart):
            results = Part.insert(tmpFile, aDoc.doc.Name)
        else:
            results = Import.insert(tmpFile, aDoc.doc.Name)

        os.remove(tmpFile)
        aDoc.setColorInfos(results)
        aDoc.replace = replace
        return aDoc
